package com.polaris.live.dialog

import android.content.Context
import androidx.lifecycle.viewModelScope
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.manager.startCountdown
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.databinding.DialogWarmingTimeBinding
import com.polaris.live.im.sfs.bean.chat.ext.RouterHighlight
import com.polaris.live.utils.route.RouterUtils

/**
 * WarmingTimeDialog
 *
 * @author Created by 半仙 on 2023/11/21/021 15:17
 */
class WarmingTimeDialog(
    context: Context,
    private val mTitle: String,
    private val mContent: String,
    private val mDuration: Long,
    private val mMastButton: String,
    private val mViewModel: BaseViewModel,
    private val highlights: List<RouterHighlight>?,
) : CenterPopupView(context) {

    private lateinit var mBinding: DialogWarmingTimeBinding

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_warming_time
    }

    override fun onCreate() {
        super.onCreate()
        mBinding = DialogWarmingTimeBinding.bind(popupImplView)
        mBinding.apply {
            tvTitle.text = mTitle

            if (highlights == null) {
                tvDetail.text = mContent
            } else {
                RouterUtils.handleText(tvDetail, mContent, highlights)
            }
            tvConfirm.setOnSingleClickListener {
                dismiss()
            }
            mViewModel.viewModelScope.startCountdown(mDuration * 1000L, 1000L, {
                tvConfirm.text = "$mMastButton${it / 1000}"
            }) {
                if (it == 0L) {
                    dismiss()
                }
            }
        }
    }
}